package com.qny.ai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qny.ai.common.ApiResponse;
import com.qny.ai.dto.chat.CreateConversationRequest;
import com.qny.ai.dto.chat.SendMessageRequest;
import com.qny.ai.dto.conversation.ConversationWithRoleDTO;
import com.qny.ai.entity.Conversation;
import com.qny.ai.entity.Message;
import com.qny.ai.service.ConversationService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/conversations")
@RequiredArgsConstructor
public class ConversationController {

    private final ConversationService conversationService;

    @PostMapping
    public ApiResponse<Long> create(@RequestBody CreateConversationRequest request, HttpServletRequest httpRequest) {
        // 从JWT token中获取用户ID
        Long userId = (Long) httpRequest.getAttribute("userId");
        System.out.println("[CONVERSATION] 创建会话请求 - userId: " + userId + ", roleId: " + request.getRoleId());
        request.setUserId(userId);
        Long conversationId = conversationService.createConversation(request);
        System.out.println("[CONVERSATION] 会话创建成功 - conversationId: " + conversationId);
        return ApiResponse.ok(conversationId);
    }

    @PostMapping("/{id}/end")
    public ApiResponse<Boolean> end(@PathVariable("id") Long id) {
        return ApiResponse.ok(conversationService.endConversation(id));
    }

    @PostMapping("/messages")
    public ApiResponse<Long> send(@Valid @RequestBody SendMessageRequest request) {
        return ApiResponse.ok(conversationService.sendUserMessage(request));
    }

    @GetMapping("/{id}/messages")
    public ApiResponse<Page<Message>> list(@PathVariable("id") Long id,
                                           @RequestParam(defaultValue = "1") int pageNo,
                                           @RequestParam(defaultValue = "20") int pageSize) {
        return ApiResponse.ok(conversationService.listMessages(id, pageNo, pageSize));
    }

    @GetMapping("/{id}")
    public ApiResponse<Conversation> detail(@PathVariable("id") Long id) {
        return ApiResponse.ok(conversationService.getById(id));
    }

    @GetMapping
    public ApiResponse<Page<ConversationWithRoleDTO>> listConversations(@RequestParam(required = false) Long roleId,
                                                                        @RequestParam(required = false) Integer active,
                                                                        @RequestParam(defaultValue = "1") int pageNo,
                                                                        @RequestParam(defaultValue = "20") int pageSize,
                                                                        HttpServletRequest httpRequest) {
        // 从JWT token中获取用户ID
        Long userId = (Long) httpRequest.getAttribute("userId");
        return ApiResponse.ok(conversationService.listConversationsWithRole(userId, roleId, active, pageNo, pageSize));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> delete(@PathVariable("id") Long id, HttpServletRequest httpRequest) {
        // 从JWT token中获取用户ID
        Long userId = (Long) httpRequest.getAttribute("userId");
        return ApiResponse.ok(conversationService.deleteConversation(id, userId));
    }
}


